import java.util.Stack;

class MySort {
    /**
     * 时间复杂度:最坏:无序数据-->O(n^2),最好：有序数据-->O(n)
     * 空间复杂度:O(1)
     * 稳定性：稳定
     * 直接插入排序
     *
     * @param array
     */
    public void insertSort(int[] array) {
        if (array == null) return;
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = 0;
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    //说明前面已经是有序的了
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    /**
     * 希尔排序
     * 时间复杂度：不好算  n^1.3 - n^1.5 之间
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     *
     * @param array
     */
    public void shellSort(int[] array) {
        if (array == null) return;
        int gap = array.length;
        //处理了gap
        while (gap > 1) {
            insertSortGap(array, gap);
            gap = gap / 3 + 1;//保证至少是1
        }
    }

    /**
     * @param array 希尔排序 要排序的数组
     * @param gap   每组数据间的间隔
     */
    public void insertSortGap(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = 0;
            for (j = i - gap; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    /**
     * 时间复杂度：
     * 最好：O(n^2)
     * 最坏：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     *
     * @param array
     */
    public void selectSort(int[] array) {
        if (array == null) return;
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[i]) {
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    /**
     * 堆排序
     * 时间复杂度：O(n*logN)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public void heapSort(int[] array) {
        if (array == null) return;
        createHeap(array);
        int end = array.length-1;
        while (end > 0) {
            int tmp = array[end];
            array[end] = array[0];
            array[0] = tmp;
            siftDown(array,0,end);
            end--;
        }
    }

    public void siftDown(int[] array, int root, int len) {
        int parent = root;
        int child = 2*parent+1;
        while (child < len) {
            //选出左右子树中最大的那个
            if (child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                int tmp = array[child];
                array[child] = array[parent];
                array[parent] = tmp;
                parent = child;
                child = 2*parent+1;
            }else{
                break;
            }
        }
    }

    /**
     * 构建大根堆
     * @param array
     */
    public void createHeap(int[] array) {
        for (int i = (array.length-1-1)/2; i >= 0; i--) {
            siftDown(array,i,array.length);
        }
    }

    /**
     *冒泡排序
     * 时间复杂度：最好最坏都是O(n^2)  但是：如果优化了 ，有序的时候就是O(n)
     * 稳定性：稳定的排序
     * @param array
     */
    public void bubbleSort(int[] array) {
        if (array == null) return;
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false) {
                break;
            }
        }
    }

    /**
     * 找基准值(pivot)
     * @param array
     * @param left
     * @param right
     * @return
     */
    public int partition(int[] array,int left,int right)  {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    public void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    //三数取中法
    public void selectPivotMedianOfThree(int[] array,int left,int right,int mid) {
        if(array[mid] > array[left]) {
            swap(array,mid,left);
        }
        if(array[left] > array[right]) {
            swap(array,left,right);
        }

    }
    public void insertSort2(int[] array,int left,int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = 0;
            for (j = i-1; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public void quick(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        //当数据小于100时候调用直接插入排序
        if (right-left+1 <= 100) {
            insertSort2(array,left,right);
            return;
        }
        int mid = left+(right-left)/2;
        selectPivotMedianOfThree(array,left,right,mid);
        int pivot = partition(array,left,right);

        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    /**
     * 快速排序
     * 时间复杂度：
     * 最好：O(n*logn)  均匀的分割下
     * 最坏：o(n^2)     数据有序的时候
     * 空间复杂度：
     * 最好：logn
     * 最坏：O(n)
     * 稳定性：不稳定的排序
     * @param array
     */
    public void quickSort(int[] array) {
        if (array == null) return;
        quick(array,0,array.length-1);
    }

    /**
     * 非递归快速排序
     * @param array
     */
    public void quickSort2(int[] array) {
        if (array == null) return;
        Stack<Integer> stack = new Stack<>();

        int left = 0;
        int right = array.length-1;
        int pivot = partition(array,0,right);
        //判断左边是否有两个元素以上
        if (pivot > left+1) {
            stack.push(0);
            stack.push(pivot-1);
        }
        if (pivot < right-1) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array,left,right);
            if (pivot > left+1) {
                stack.push(0);
                stack.push(pivot-1);
            }
            if (pivot < right-1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度：O(N*log n)
     * 空间复杂度：O(n)
     * 稳定性：稳定的排序
     * @param array
     */
    public void mergeSort(int[] array) {
        if (array == null) return;
        mergeSortInternal(array,0,array.length-1);
    }
    public void mergeSortInternal(int[] array, int low, int high) {
        if (low >= high) {
            return;
        }
        int mid = low+(high-low)/2;
        mergeSortInternal(array,low,mid);
        mergeSortInternal(array,mid+1,high);
        merge(array,low,mid,high);
    }

    /**
     * 合并分解的有序数组
     * @param array
     * @param low
     * @param mid
     * @param high
     */
    public void merge(int[] array, int low, int mid, int high) {
        int[] tmp = new int[high-low+1];//存储归并好的数据

        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        int k = 0;//数组的下标
        while (s1 <= e1 && s2 <= e2) {
            // 加等于，保证稳定性
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else{
                tmp[k++] = array[s2++];
            }
        }
        //左边还有元素
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        //右边还有元素
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //把排序好的数组搬回原数组
        for (int i = 0; i < tmp.length; i++) {
            array[i+low] = tmp[i];
        }
    }

    /**
     * 非递归归并排序
     * @param array
     */
    public void mergeSort2(int[] array) {
        if (array == null) return;
        for (int i = 1; i < array.length; i*=2) {
            merge2(array,i);
        }
    }

    /**
     *
     * @param array
     * @param gap 每组的个数
     */
    public void merge2(int[] array,int gap) {
        int[] tmp = new int[array.length];
        int k = 0;
        int s1 = 0;
        int e1 = s1+gap-1;
        int s2 = e1+1;
        int e2 = s2+gap-1 >= array.length ? array.length-1 : s2+gap-1;

        while (s2 < array.length-1) {
            while (s1 <= e1 && s2 <= e2) {
                if (array[s1] <= array[s2]) {
                    tmp[k++] = array[s1++];
                }else {
                    tmp[k++] = array[s2++];
                }
            }
            while (s1 <= e1) {
                tmp[k++] = array[s1++];
            }
            while (s2 <= e2) {
                tmp[k++] = array[s2++];
            }
            s1 = e2+1;
            e1 = s1+gap-1;
            s2 = e1+1;
            e2 = s2+gap-1 >= array.length ? array.length-1 : s2+gap-1;
        }
        while (s1 <= array.length-1) {
            tmp[k++] = array[s1++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[i] = tmp[i];
        }
    }
}
